using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Pheonix.CodeLibrary.Web;
using System.Data.SqlTypes;
using Pheonix.CodeLibrary;
using Pheonix.Fashionzoomer.DAL;

public partial class page_private_admin_membershipdetail : AdminBasePage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Title = SiteName + " - Membership Detail [ Admin ]";
        LoadScript();
        BindControl();
        if (!IsPostBack)
        {
            try
            {
                if (Request["id"] != null)
                {
                    int id = 0;
                    if (int.TryParse(Request["id"], out id))
                        LoadEditData(id);
                }
            }
            catch (Exception ex)
            {
                uccResponseMessage.SetFailMessage("There was a problem in loading the page");
            }
        }
        if (Request["id"] == null)
            lblTitle.Text = "Add a new membership package";
        else
            lblTitle.Text = "Update membership package";
    }

    private void BindControl()
    {
        tbxExpireDate.Attributes.Add("datepicker", "true");
        tbxExpireDate.Attributes.Add("datepicker_format", "MM-DD-YYYY");
    }

    private void LoadScript()
    {
        if (!ClientScript.IsClientScriptIncludeRegistered("datepickercontrol"))
            ClientScript.RegisterClientScriptInclude("datepickercontrol", "../thirdparty/datepickercontrol/datepickercontrol.js");
    }

    private void LoadEditData(int editDataId)
    {
        Pheonix.Fashionzoomer.Entity.SqlServer2000.Membership editMembership = null;
        if (editDataId != 0)
            editMembership = Pheonix.Fashionzoomer.Entity.SqlServer2000.Membership.GetMembershipById(editDataId);
        if (editMembership != null)
        {
            tbxPackageName.Text = editMembership.PackageName;
            tbxCost.Text = editMembership.PackageCost.ToString();
            tbxExpireDate.Text = editMembership.ExpireDate.ToShortDateString();     
        }
    }

    protected void Save_Click(object sender, ImageClickEventArgs e)
    {
        if (Page.IsValid)
        {
            int membershipPackageId = 0;
            Pheonix.Fashionzoomer.Entity.SqlServer2000.Membership editmembershipPackage = null;
            string oldPackageName = string.Empty;
            if (Request["id"] != null)
            {
                if (int.TryParse(Request["id"], out membershipPackageId))
                {
                    editmembershipPackage = Pheonix.Fashionzoomer.Entity.SqlServer2000.Membership.GetMembershipById(membershipPackageId);
                    if (editmembershipPackage != null)
                        oldPackageName = editmembershipPackage.PackageName;
                }
            }
            string packageName = Utility.FilterInputText(tbxPackageName.Text);
            if (packageName == oldPackageName || CustomCommand.IsPackageNameFree(packageName))
            {
                string packageCost = Utility.FilterInputText(tbxCost.Text);
                string expireDate = Utility.FilterInputText(tbxExpireDate.Text);
                DateTime expireDateValue = SqlDateTime.MaxValue.Value;
                decimal packageCostValue = 0;
                decimal.TryParse(packageCost, out packageCostValue);
                DateTime.TryParse(expireDate, out expireDateValue);

                if (editmembershipPackage != null)
                {
                    editmembershipPackage.PackageCost = packageCostValue;
                    editmembershipPackage.ExpireDate = expireDateValue;

                    if (editmembershipPackage.Save())
                    {
                        uccResponseMessage.SetSuccessMessage("Membership package successfuly updated");
                        LoadEditData(membershipPackageId);
                    }
                    else
                        uccResponseMessage.SetFailMessage("Could not update membership package");
                }
                else
                {
                    int newMembershipPackageId = Pheonix.Fashionzoomer.Entity.SqlServer2000.Membership.CreateNewMembership(packageName, 
                        expireDateValue, packageCostValue);
                    if (newMembershipPackageId > 0)
                    {
                        uccResponseMessage.SetSuccessMessage("Membership package successfuly created");
                        LoadEditData(newMembershipPackageId);
                    }
                    else
                        uccResponseMessage.SetFailMessage("Could not create membership package");
                }
            }
            else
                uccResponseMessage.SetFailMessage("Membership package name is already taken");
        }
        else
            uccResponseMessage.SetFailMessage("Could not save because of invalid data");
    }

    protected void Back_Click(object sender, ImageClickEventArgs e)
    {
        Response.Redirect("membershiplist.aspx");
    }
}
